package com.atgm.mall.gmmailproduct.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailproduct.entity.SkuImagesEntity;
import com.atgm.mall.gmmailproduct.feign.SeckillFeignService;
import com.atgm.mall.gmmailproduct.service.*;
import com.atgm.mall.gmmailproduct.vo.SeckillSkuVo;
import com.atgm.mall.gmmailproduct.vo.SkuItemSaleAttrVo;
import com.atgm.mall.gmmailproduct.vo.SkuItemVo;
import com.atgm.mall.gmmailproduct.vo.SpuItemAttrGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;

import com.atgm.mall.gmmailproduct.dao.SkuInfoDao;
import com.atgm.mall.gmmailproduct.entity.SkuInfoEntity;

@Slf4j
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;


    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (GmMallUtils.isNotEmpty(key)) {
            queryWrapper.and(wapper -> {
                wapper.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        String brandId = (String) params.get("brandId");
        if (GmMallUtils.isNotEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String catelogId = (String) params.get("catelogId");
        if (GmMallUtils.isNotEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            queryWrapper.eq("catalog_id", catelogId);
        }
        String min = (String) params.get("min");
        if (GmMallUtils.isNotEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        String max = (String) params.get("max");
        if (GmMallUtils.isNotEmpty(brandId)) {
            if (new BigDecimal(max).compareTo(new BigDecimal("0")) == 1) {
                queryWrapper.le("price", max);
            }
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> entityList = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return entityList;
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        SkuItemVo skuItemVo = new SkuItemVo();
        //异步执行
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //1、sku基本信息
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, threadPoolExecutor);

        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((res) -> {
            //3、获取spu的销售属性组合
            List<SkuItemSaleAttrVo> saleAttr = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttr);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuAttrFuture = infoFuture.thenAcceptAsync((res) -> {
            //5、spu的规格参数
        }, threadPoolExecutor);

        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            //2、sku的图片信息
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        }, threadPoolExecutor);

        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
        }, threadPoolExecutor);

        //完成所有异步任务返回
        CompletableFuture.allOf(saleFuture, spuAttrFuture, imgFuture, seckillFuture).get();

        long end = System.currentTimeMillis();

        log.info("花费时间 : {} 毫秒",(end - start));
        return skuItemVo;
    }
}